package captor.modelsystem.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Observable;
import java.util.Properties;

import captor.lib.def.Constant;
import captor.modelsystem.Model;

/**
 * @author Kicho
 *
 */
public class ProjectDefaultConfig extends Observable {
    
    private boolean overwriteResources;
    private Model model;
    private Properties applicationProps;
    
    public ProjectDefaultConfig(Model model) {
        this.model = model;
    }
    
    //-------------------------------------------------------------------------
    
    /**
     * @return Returns the overwrite.
     */
    public boolean getOverwriteResources() {
        return overwriteResources;
    }
    /**
     * @param overwrite The overwrite to set.
     */
    public void setOverwriteResources(boolean overwrite) {
        this.overwriteResources = overwrite;
    }
    
    //-------------------------------------------------------------------------
    
    public void loadDefault(boolean production)  {
        
        String ppath = model.getConfig().getSystemConfig().getProjectDefaultConfigPath();
        if ( production )  {
            Properties defaultProps;
            try  {
                defaultProps = new Properties();
                FileInputStream in = new FileInputStream(ppath);
                defaultProps.load(in);
                in.close();        
                
                applicationProps = new Properties(defaultProps);
                try {
                    overwriteResources = new Boolean(applicationProps.getProperty("overwriteResources")).booleanValue();
                } catch (RuntimeException e1) {
                    overwriteResources = false;
                }
            }
            catch(Exception e)  {
                model.getGui().getGuiView().setErrorView("Captor Exception: Cannot open properties files.<br>Check if the files " + ppath + " exists.<br>");
                return;
            }
        }
        else  {
            overwriteResources = false;        
        }
        
    }
    
    //-------------------------------------------------------------------------
    
    public void load(String projectFilePath) {
        
        String ppath = model.getConfig().getSystemConfig().getProjectPropertiesDefaultPath();
        
        Properties defaultProps;
        try  {
            defaultProps = new Properties();
            FileInputStream in = new FileInputStream(ppath);
            defaultProps.load(in);
            in.close();        
            
            applicationProps = new Properties(defaultProps);
            
            File file = new File(projectFilePath);
            if ( !file.exists() )  {
                applicationProps = defaultProps;
            }
            else  {
                in = new FileInputStream(projectFilePath);
                applicationProps.load(in);
                in.close();
            }
        }
        catch(Exception e)  {
            return;
        }
        
        try {
            overwriteResources = new Boolean(applicationProps.getProperty("overwriteResources")).booleanValue();
        } catch (RuntimeException e1) {
            overwriteResources = false;
        }
        setChanged();
        notifyObservers(Constant.LOAD_PROJECT);
    }
    
    //-------------------------------------------------------------------------
    
    public void save()  {
        String ppath = model.getConfig().getSystemConfig().getProjectPropertiesDefaultPath();
        try  {
            FileOutputStream out = new FileOutputStream(ppath);
            applicationProps.put("overwriteResources", new Boolean(overwriteResources).toString());
            applicationProps.store(out, "---DO NOT EDIT THIS FILE---");
            out.close();        
        }
        catch(Exception e)  {
            model.getGui().getGuiView().setErrorView("Captor Exception: Cannot save project properties file: " + ppath + "<br>");
            return;
        }
    }
    
    //-------------------------------------------------------------------------
}
